package com.hzdesk.media.koreabatchmatvector.logic.persenter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.hzdesk.media.koreabatchmatvector.adapter.ColorPickerAdapter;
import com.hzdesk.media.koreabatchmatvector.logic.contract.ColorPickerContract;
import com.hzdesk.media.koreabatchmatvector.logic.view.QuickStartActivity;
import com.hzdesk.media.koreabatchmatvector.utils.FileUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/21.
 */

public class ColorPickerPersenter implements ColorPickerContract.IColorPickerPersenter {
    private Context mContext;
    private RecyclerView mRecyclerView;
    private List<List<String>> mList;
    private ColorPickerContract.IColorPickerView iColorPickerView;
    private ColorPickerAdapter adapter;

    public ColorPickerPersenter(ColorPickerContract.IColorPickerView iColorPickerView) {
        this.iColorPickerView = iColorPickerView;
    }

    @Override
    public void initView(Context context, Intent intent, RecyclerView recyclerView) {
        mContext = context;
        mRecyclerView = recyclerView;
        mList = new ArrayList<>();
        adapter = new ColorPickerAdapter(mContext);
        initDate();
        adapter.setDate(mList);
        LinearLayoutManager layoutmanager = new LinearLayoutManager(mContext);
        layoutmanager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutmanager);
        mRecyclerView.setAdapter(adapter);
        adapter.setOnColorSelectListener(new ColorPickerAdapter.onColorSelectListener() {
            @Override
            public void select(String color) {
                Log.d("lk", color);
                iColorPickerView.onColorSelect(color);
            }
        });
    }

    @Override
    public void onClickedBack() {
        iColorPickerView.back();
    }

    /**
     * 分割字符串
     */
    private void initDate() {
        String colorString = readAssetsTxt(mContext, "colorpicker");
        String[] all = colorString.split("-");
        for (String s : all) {
            String[] spilt = s.split("#");
            List<String> list = new ArrayList<>();
            for (String s2 : spilt) {
                list.add(s2.trim());
            }
            mList.add(list);
        }


    }

    /**
     * 读取assets 下的文件
     * @param context
     * @param fileName
     * @return
     */
    public static String readAssetsTxt(Context context, String fileName) {
        try {
            InputStream is = context.getResources().getAssets().open(fileName + ".txt");
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            String text = new String(buffer, "utf-8");
            return text;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "读取错误，请检查文件名";
    }


}
